<?php
    class ARingBack extends CActiveRecord
    {
        public static function model($className = __CLASS__)
        {
            return parent::model ( $className );
        }
        // goi den bang can ket noi   
        public function tableName() 
        {                  
            return 'c_ring_back';
        }
        
        public function getAllRingBack($id)
        {
            $sql = "SELECT * FROM c_ring_back  WHERE id = ".$id;
            $connect = Yii::app()->db;
            $command = $connect->createCommand($sql);
            $data = $command->queryAll();
            return $data;
        }               

        public function getAllDataGen()
        {
            $sql = "SELECT DISTINCT name FROM c_ring_singer ORDER BY name ";
            $connect = Yii::app()->db;
            $command = $connect->createCommand($sql);
            $data = $command->queryAll();
            return $data;
        }               
        public function getDataSearch($keyword,$type,$status,$hit,$lyrics,$page,$row_per_page,$order_download,$type,$singerId,$categoryId,$data_categoryId,$data_code,$ringId)
        {
            $str_sql = "";
            $str_order = " ORDER BY t1.id DESC";
            if($singerId > 0){
                $str_sql .= " AND t1.singerId = ".$singerId;    
            }
            if($categoryId > 0){
                $str_sql .= " AND t1.categoryId = ".$categoryId;    
            }  
            if($keyword !="" && intval($type) >0){
                if($type == 1){
                    $str_sql .= " AND t1.id = ".intval($keyword);
                }
                if($type == 2){
                    $str_sql .= " AND t1.title LIKE '%".$keyword."%' "; 
                }
            } 
            if(intval($status) != 2){
                $str_sql .= " AND t1.status = ".intval($status);
            }
            if(intval($lyrics) != 2){
                $str_sql .= " AND t1.lyrics = ".intval($lyrics);
            }
            if(intval($hit) != 2){
                $str_sql .= " AND t1.hit = ".intval($hit);
            }
            if($order_download >0){
                if($order_download==1){
                    $str_order = " ORDER BY t1.download ASC";
                }else{
                    $str_order = " ORDER BY t1.download DESC";   
                }
            }
            $connect = Yii::app()->db;
            $sql = "SELECT count(t1.id) as count FROM c_ring_back t1 INNER JOIN c_ring_singer t2 ON t1.singerId = t2.id WHERE 1 ".$str_sql."";
            $command = $connect->createCommand($sql);
            $data_count = $command->queryRow();
            $max_page = ceil(intval($data_count["count"])/$row_per_page);
            $first = ($page - 1)*$row_per_page;
            $sql = "SELECT t1.*, t2.name FROM c_ring_back t1 INNER JOIN c_ring_singer t2 ON t1.singerId = t2.id WHERE 1 ".$str_sql." ".$str_order." LIMIT ".$first.",".$row_per_page."";
            $command = $connect->createCommand($sql);
            $data = $command->queryAll();
            return array($max_page,intval($data_count["count"]),$data);
        }

        public function getDataSearchSinger($keyword,$type,$status,$page,$row_per_page,$order_download,$name){
            $str_sql = "";
            $str_order = " ORDER BY id DESC";
            if($keyword !="" && intval($type) >0){
                if($type == 1){
                    $str_sql .= " AND id = ".intval($keyword);
                }
                if($type == 2){
                    $str_sql .= " AND name LIKE '%".$keyword."%' "; 
                }
            }
            if($name !=""){
                 $str_sql .= " DISTINCT name LIKE '%".$name."%'"; 
            }
            if(intval($status) != 2){
                $str_sql .= " AND status = ".intval($status); 
            }
            if($order_download >0){
                if($order_download==1){
                    $str_order = " ORDER BY id ASC";
                }else{
                    $str_order = " ORDER BY id DESC";   
                }
            }
            $connect = Yii::app()->db;
            $sql = "SELECT count(id) as count FROM c_ring_singer WHERE 1 ".$str_sql."";
            $command = $connect->createCommand($sql);
            $data_count = $command->queryRow();
            $max_page = ceil(intval($data_count["count"])/$row_per_page);
            $first = ($page - 1)*$row_per_page;
            $sql = "SELECT * FROM c_ring_singer WHERE 1 ".$str_sql." ".$str_order." LIMIT ".$first.",".$row_per_page."";
            $command = $connect->createCommand($sql);
            $data = $command->queryAll();
            return array($max_page,intval($data_count["count"]),$data);
        }
        
        public function getSearchCategory($keyword,$type,$status,$page,$row_per_page,$order_download){
            $str_sql = "";
            $str_order = " ORDER BY id DESC";
            if($keyword !="" && intval($type) >0){
                if($type == 1){
                    $str_sql .= " AND id = ".intval($keyword);
                }
                if($type == 2){
                    $str_sql .= " AND title LIKE '%".$keyword."%' "; 
                }
            }
            if(intval($status) != 2){
                $str_sql .= " AND status = ".intval($status);
            }
            if($order_download >0){
                if($order_download==1){
                    $str_order = " ORDER BY id ASC";
                }else{
                    $str_order = " ORDER BY id DESC";   
                }
            }
            $connect = Yii::app()->db;
            $sql = "SELECT count(id) as count FROM c_ring_category WHERE 1 ".$str_sql."";
            $command = $connect->createCommand($sql);
            $data_count = $command->queryRow();
            $max_page = ceil(intval($data_count["count"])/$row_per_page);
            $first = ($page - 1)*$row_per_page;
            $sql = "SELECT * FROM c_ring_category WHERE 1 ".$str_sql." ".$str_order." LIMIT ".$first.",".$row_per_page."";
            $command = $connect->createCommand($sql);
            $data = $command->queryAll();
            return array($max_page,intval($data_count["count"]),$data);
        }

        public function getDataByCategoryAndType($categoryId){
            $sql = "SELECT * FROM c_ring_back WHERE id = ".$categoryId;
            $connect = Yii::app()->db;
            $command = $connect->createCommand($sql);
            $data = $command->queryAll();
            return $data;
        } 
        public function getDataById($id){ 
            $sql = "SELECT * FROM c_ring_back WHERE id = ".$id;  
            $connect = Yii::app()->db;
            $command = $connect->createCommand($sql);
            $data = $command->queryRow();
            return $data;
        } 
        
        public function getDataCodeId($ringId){ 
            $sql = "SELECT * FROM c_ring_back WHERE id = ".$ringId;  
            $connect = Yii::app()->db;
            $command = $connect->createCommand($sql);
            $data = $command->queryRow();
            return $data;
        }     


    }
?>
